package com.mxnet.chongjin.nci.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.mxnet.chongjin.nci.R;
import com.mxnet.chongjin.nci.base.BaseActivity;
import com.mxnet.chongjin.nci.config.Api;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONObject;

import okhttp3.Call;

public class ChangePasswordActivity extends BaseActivity {
    private Context mContext;
    private Button mButton;
    private TextView mTextView;
    private EditText mEditText1, mEditText2, mEditText3;
    private SharedPreferences mPreferences;
    private SharedPreferences.Editor mEditor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        mContext = ChangePasswordActivity.this;
        mPreferences = getSharedPreferences("nci", MODE_PRIVATE);
        mEditor = mPreferences.edit();

        mTextView = findViewById(R.id.textview_change_password);
        mTextView.setText(mPreferences.getString("username", ""));

        mEditText1 = findViewById(R.id.edittext_change_password1);

        mEditText2 = findViewById(R.id.edittext_change_password2);

        mEditText3 = findViewById(R.id.edittext_change_password3);

        mButton = findViewById(R.id.btn_change_password);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mEditText1.getText().toString())) {
                    Toast.makeText(mContext, "请输入原密码！", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(mEditText2.getText().toString())) {
                    Toast.makeText(mContext, "请输入新密码！", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(mEditText3.getText().toString())) {
                    Toast.makeText(mContext, "请确认新密码！", Toast.LENGTH_SHORT).show();
                } else if (!(mEditText2.getText().toString().equals(mEditText3.getText().toString()))) {
                    Toast.makeText(mContext, "两次输入新密码不一致！", Toast.LENGTH_SHORT).show();
                } else {
                    Log.i("*****==onClick==", mEditText1.getText().toString() + mEditText2.getText().toString());
                    OkHttpUtils.get()
                            .url(mPreferences.getString("URL_HEADER", "") + Api.CHANGE_PASSWORD)
                            .addParams("userName", mPreferences.getString("username", ""))
                            .addParams("oldPassword", mEditText1.getText().toString())
                            .addParams("newPassword", mEditText2.getText().toString())
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int id) {
                                    Log.i("*****==onError==", e.getMessage());
                                }

                                @Override
                                public void onResponse(String response, int id) {
                                    if (!TextUtils.isEmpty(response)) {
                                        try {
                                            JSONObject jsonObject = new JSONObject(response);
                                            boolean isSuccess = jsonObject.optBoolean("success");
                                            if (isSuccess) {
                                                Toast.makeText(mContext, jsonObject.optString("message"), Toast.LENGTH_SHORT).show();
                                                mEditor.putString("username", "");
                                                mEditor.putBoolean("isLogin", false);
                                                mEditor.commit();
                                                Intent intent = new Intent(mContext, LoginActivity.class);
                                                startActivity(intent);
                                                finish();
                                            } else {
                                                Toast.makeText(mContext, jsonObject.optString("message"), Toast.LENGTH_SHORT).show();
                                            }
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            });
                }
            }
        });
    }
}
